import { updateData, ifWorldData, ifEnemyData, ifPlayerData, ifIdOpr, ifIdPos} from "./dataBase";

export class worldData {

    private _data: ifWorldData;

    private _enemyKey:number = 0;
    private _playerKey:number = 0;

    public get data() {
        var data = this._data;
        return data;
    }

    @updateData("WORLD_DATA")
    public Data(data:ifWorldData){
        this._data = data;
        this._enemyKey = 0;
        this._playerKey = 0;
        return this._data;
    }
    @updateData("WORLD_DATA", "ADD_ENEMY")
    public ADD_ENEMY(data:ifEnemyData){
        this._data.enemyList[this._enemyKey] = data;
        this._data.enemyList[this._enemyKey].id = this._enemyKey;
        var enemyData = this._data.enemyList[this._enemyKey];
        this._enemyKey++;
        return enemyData;
    }
    @updateData("WORLD_DATA", "ADD_PLAYER")
    public ADD_PLAYER(data:ifPlayerData){
        this._data.playerList[this._playerKey] = data;
        this._data.playerList[this._playerKey].id = this._playerKey;
        var playerData = this._data.playerList[this._playerKey];
        this._playerKey++;
        return playerData;
    }
    @updateData("WORLD_DATA", "DEL_ENEMY")
    public DEL_ENEMY(id:number){
        this._data.enemyList[id] = null;
        return {idKey:id};
    }
    @updateData("WORLD_DATA", "DEL_PLAYER")
    public DEL_PLAYER(id:number){
        this._data.playerList[id] = null;
        return {idKey:id};
    }
    @updateData("WORLD_DATA", "ENEMY_HP")
    public ENEMY_HP(data:ifIdOpr){
        var _enemyData = this._data.enemyList[data.id];
        if(!_enemyData) return null;
        _enemyData.hp = data.amount;
        return {idKey:data.id, Hp:_enemyData.hp};
    }
    @updateData("WORLD_DATA", "ENEMY_HP")
    public ENEMY_HP_OPR(data:ifIdOpr){
        var _enemyData = this._data.enemyList[data.id];
        if(!_enemyData) return null;
        _enemyData.hp += data.amount;
        return {idKey:_enemyData.id, Hp:_enemyData.hp};
    }
    @updateData("WORLD_DATA", "PLAYER_HP")
    public PLAYER_HP(data:ifIdOpr){
        var _playerData = this._data.enemyList[data.id];
        if(!_playerData) return null;
        _playerData.hp = data.amount;
        return {idKey:_playerData.id, Hp:_playerData.hp};
    }
    @updateData("WORLD_DATA", "PLAYER_HP")
    public PLAYER_HP_OPR(data:ifIdOpr){
        var _playerData = this._data.enemyList[data.id];
        if(!_playerData) return null;
        _playerData.hp += data.amount;
        return {idKey:_playerData.id, Hp:_playerData.hp};
    }
    @updateData("WORLD_DATA", "ENEMY_POS")
    public ENMEY_POSITION(data:ifIdPos)
    {
        var _enemyData = this._data.enemyList[data.id];
        if(!_enemyData) return null;
        _enemyData.pos = data.pos;
        return {idKey:_enemyData.id, Pos:_enemyData.pos}
    }
    @updateData("WORLD_DATA", "KILL_COUNT")
    public KILL_COUNT(count:number)
    {
        this._data.killCount = count;
        return this._data.killCount;
    }
    @updateData("WORLD_DATA", "KILL_COUNT")
    public KILL_COUNT_OPR(count:number)
    {
        this._data.killCount += count;
        return this._data.killCount;
    }
}
